<?php

namespace App\Http\Controllers\Admin;

use App\Models\City;
use App\Models\Fang;
use GuzzleHttp\Client; //发起http请求
use Illuminate\Http\Request;
use App\Http\Requests\FangRequest;
// 导入es生成索引类
use Elasticsearch\ClientBuilder;

class FangController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        $url = config('gaode.geocode');
//
//        $url = sprintf($url,'东瓜山一村','长沙市');
//        $client = new Client(['timeout'=>5]);
//
//        //发起请求
//        $response = $client->get($url);
//        $body = (string)$response->getBody();
//        dd($body);
//
//        $arr = json_decode($body,true);
//
//        //如果找到了对应的经纬度。存入数据库
//        if(count($arr['geocodes'])>0){
//            $locationArr = explode(',',$arr['geocodes'][0]['location']);
//            dd($locationArr);
////            $model->update([
////                'longitude'=>$locationArr[0],
////                'latitude'=>$locationArr[1],
////            ]);
//        }
//        dd($url);
        //with 关联关系的调用
        $data = Fang::with(['owner'])->paginate($this->pagesize);
        return view('admin.fang.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Fang $fang)
    {
       $data = $fang->relationData(); //依赖注入
        return view('admin.fang.create',$data);
    }

    public function city(Request $request)
    {
       $data =  City::where('pid',$request->input('id'))->get(['id','name']);
       return $data;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(FangRequest $request)
    {
        $dopost = $request->except(['_token','file']);
        //入库
        $model = Fang::create($dopost);
        //发起http请求
        //申明一个请求类，并指定过期时间
        $client = new Client(['timeout'=>5]);
        $url = config('gaode.geocode');
        $url = sprintf($url,$model->fang_addr,$model->fang_province);
        //发起请求
        $response = $client->get($url);
        $body = (string)$response->getBody();
        $arr = json_decode($body,true);
        //如果找到了对应的经纬度。存入数据库
        if(count($arr['geocodes'])>0){
            $locationArr = explode(',',$arr['geocodes'][0]['location']);
            $model->update([
                'longitude'=>$locationArr[0],
                'latitude'=>$locationArr[1],
            ]);
        }
        //es数据的添加
        //得到es客户端对象
        $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
        // 写文档
        $params = [
            'index' => 'fang',
            'type' => '_doc',
            'id' => $model->id,
            'body' => [
                'fang_name' => $model->fang_name,
                'fang_desn' => $model->fang_desn,
            ],
        ];
        // 添加数据到索引文档中
        $client->index($params);
        return redirect(route('admin.fang.index'));
    }
    //生成房源信息索引
    public function esinit()
    {
        // 得到es客户端对象
        $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
        // 创建索引
        $params = [
            // 生成索引的名称
            'index' => 'fang',
            // 类型 body
            'body' => [
                'settings' => [
                    // 分区数 设置之后不能再加
                    'number_of_shards' => 5,
                    // 副本数
                    'number_of_replicas' => 1
                ],
                'mappings' => [
                    '_doc' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        // 字段  类似表字段，设置类型
                        'properties' => [
                            'fang_name' => [
                                // 相当于数据查询是的 = 张三你好，必须找到张三你好
                                'type' => 'keyword'
                            ],
                            'fang_desn' => [
                                'type' => 'text',
                                // 中文分词  张三你好   张三  你好 张三你好
                                'analyzer' => 'ik_max_word',
                                'search_analyzer' => 'ik_max_word'
                            ]
                        ]
                    ]
                ]
            ]
        ];
        // 创建索引
        $response = $client->indices()->create($params);

        dump($response);
    }
        //改变房源状态
    public function changestatus(Request $request)
    {
        $id = $request->input('id');
        $status = $request->input('status');
        Fang::where('id',$id)->update(['fang_status'=>$status]);
        return ['status'=>0,'msg'=>'修改成功'];

    }
    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function show(Fang $fang)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function edit(Fang $fang)
    {
        //
        $data = $fang->relationData(); //依赖注入
        // 得到当前用户所属省对应的市列表
        $currentCityData = City::where('pid', $fang->fang_province)->get();
        // 得到当前用户所属市对应的区列表
        $currentRegionData = City::where('pid', $fang->fang_city)->get();

        $data['currentCityData'] = $currentCityData;
        $data['currentRegionData'] = $currentRegionData;
        $data['fang'] = $fang;
//        dd($data);
        return view('admin.fang.edit',$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Fang $fang)
    {
        //
        // 接受表单数据
        $putData = $request->except(['_method','_token','file']);
        // 修改入库
        $fang->update($putData);

        // 修改经纬度
        // 发起HTTP请求
        // 申明一个请求类，并指定请求的过期时间
        $client = new Client(['timeout' => 5]);
        // 得到请求地址
        $url = config('gaode.geocode');
        $url = sprintf($url, $fang->fang_addr, $fang->fang_province);
        // 发起请求
        $response = $client->get($url);
        $body = (string)$response->getBody();
        $arr = json_decode($body, true);
        // 如果找到了对应经纬度，存入数据表中
        if (count($arr['geocodes']) > 0) {
            $locationArr = explode(',', $arr['geocodes'][0]['location']);
            $fang->update([
                'longitude' => $locationArr[0],
                'latitude' => $locationArr[1]
            ]);
        }

        // es数据的修改
        // 得到es客户端对象
        $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
        // 修改文档
        $params = [
            'index' => 'fang',
            'type' => '_doc',
            // 只需要ID存在就修改
            'id' => $fang->id,
            'body' => [
                'fang_name' => $fang->fang_name,
                'fang_desn' => $fang->fang_desn,
            ],
        ];
        // 修改数据到索引文档中
        $client->index($params);

        // 跳转
        return redirect(route('admin.fang.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fang  $fang
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fang $fang)
    {
        //
    }

    public function upfile(Request $request)
    {
        //默认图标
        $pic = config('up.pic');
        if($request->hasFile('file')){
            //参数2 配置的节点名称
            $ret = $request->file('file')->store('','fang');
            $pic = '/uploads/fang/'.$ret;
        }
        return ['status'=>0,'url'=>$pic];
    }

    public function delfile(Request $request)
    {
        $filepath = $request->get('file');
        //得到真实地址
        $path = public_path().$filepath;
        //删除指定文件
        unlink($path);
        return ['status'=>0,'msg'=>'成功'];
    }
}
